#include "commt.h"

int main()
{
    key_t key = ftok(FILENAME, PROJ_ID);
    if (key < 0)
    {
        printf("ftok fail!\n");
        exit(1);
    }

    int shmid = shmget(key, SIZE, IPC_CREAT);//有共享内存，就直接返回共享内存的id
    if (shmid < 0)
    {
        printf("shmget fail!\n");
        exit(2);
    }

    std::cout << "key: " << key << std::endl;
    std::cout << "shmid: " << shmid << std::endl;

    // 挂接
    printf("shmat begin!\n");
    char *mem = (char *)shmat(shmid, NULL, 0);
    if (mem == (void *)-1)
    {
        printf("shmat fail!\n");
        exit(3);
    }
    printf("shmat success!\n");
    int i = 0;
    // 客户端这里不断向服务器写入数据
    while (1)
    {
        mem[i] = 'A' + i;
        i++;
        mem[i] = '\0';
        sleep(1);
    }

    // 去关联
    shmdt(mem);
    return 0;
}